import qs from 'qs';
import { ref, reactive, nextTick, onMounted } from "vue"
import { type PaginationProps } from "@pureadmin/table";
import { CaseTestGetAll, CaseTestLoadCaseTest, CaseTestDeleteCaseTest, UpdateCaseTest } from "@/api/admin/bigDataCase"
import { ElMessage, ElMessageBox, FormInstance } from "element-plus";
import { emitter } from "@/utils/mitt";
export function useEvaluation() {
  const loading = ref<boolean>(false);
  const dataList = ref<object | undefined>([]);
  const isdisabled = ref<boolean>(false)
  const columns: TableColumnList = [
    {
      label: "序号",
      type: "index",
      minWidth: 100
    }, {
      label: "题型",
      prop: "questionType",
      minWidth: 120,
      cellRenderer: ({ row }) => (
        <el-tag type={
          row.questionType === '1' ? "" :
            row.questionType === '2' ? "warning" : "danger"}>
          {
            row.questionType === '1' ? "单选题" :
              row.questionType === '2' ? "多选题" : "判断题"
          }
        </el-tag>
      )
    }, {
      label: "题目",
      prop: "title",
      minWidth: 250
    }, {
      label: "正确答案",
      prop: "trueAnswer",
      minWidth: 120
    }, {
      label: "操作",
      fixed: "right",
      width: 180,
      slot: "operation"
    }
  ];
  const SearchMenuId = ref<any>(null);
  const UpdateVisible = ref<boolean>(false);
  const loadingTitle = ref<string>('添加');
  // 分页
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    background: true
  });
  //  题型
  const QuestionType = [
    { id: 1, title: "单选题" },
    { id: 2, title: "多选题" },
    { id: 3, title: "判断题" },
  ];

  // 校验
  const rules = {
    questionType: [
      {
        required: true,
        message: "请选择题型！",
        trigger: ["blur", "change"]
      }
    ],
    title: [
      {
        required: true,
        message: "请输入题目名称！",
        trigger: ["blur", "change"]
      }
    ],
    trueAnswer: [
      {
        required: true,
        message: "请设置答案",
        trigger: ["blur", "change"]
      }
    ]
  }

  // from提交的值
  const updatefrom = reactive({
    caseTestId: "",
    menuId: "",
    questionType: "",
    title: "",
    answerA: "",
    answerB: "",
    answerC: "",
    answerD: "",
    answerE: "",
    answerF: "",
    trueAnswer: ""
  })
  // 兄弟传值-接
  emitter.on("getEvalList", (row) => {
    SearchMenuId.value = row;
    onSearch()
  })
  async function onSearch() {
    const data = {
      menuId: SearchMenuId.value.menuId,
      pageIndex: pagination.currentPage,
      pageSize: pagination.pageSize
    }
    const menuId = qs.stringify(data);
    const respone = await CaseTestGetAll(menuId);
    if (respone.success) {
      dataList.value = respone.data.list;
      pagination.total = respone.data.totalCount;
    } else {
      ElMessage.error(respone.msg)
    }
  }
  // 添加 or 修改
  function DetailsUpdate(row: any | undefined) {
    if (!SearchMenuId.value) return ElMessage.warning("请选择其中一条案例");
    UpdateVisible.value = true;
    if (row) {
      loadingTitle.value = '修改';
      nextTick(() => {
        updatefrom.caseTestId = row.caseTestId;
        updatefrom.menuId = row.menuId;
        updatefrom.questionType = row.questionType === '1' ? "单选题" :
          row.questionType === '2' ? "多选题" : "判断题";
        updatefrom.title = row.title;
        updatefrom.answerA = row.answerA;
        updatefrom.answerB = row.answerB;
        updatefrom.answerC = row.answerC;
        updatefrom.answerD = row.answerD;
        updatefrom.answerE = row.answerE;
        updatefrom.answerF = row.answerF;
        updatefrom.trueAnswer = row.trueAnswer;
        isdisabled.value = true;
      })
      return;
    }
    isdisabled.value = false;
    updatefrom.caseTestId = "";
    loadingTitle.value = '添加';
  };

  // 保存题目
  function addEvaluation(row: any | undefined) {
    (row as any).validate(async valid => {
      if (!valid) return;
      const data = { ...updatefrom, ...{ menuId: SearchMenuId.value.menuId } }
      const respone = await UpdateCaseTest(data);
      if (respone.success) {
        onSearch();
        UpdateVisible.value = false;
        row.resetFields()
      } else {
        ElMessage.error(respone.msg)
      }
    })
  }

  // 删除
  function handleDelete(row: any) {
    ElMessageBox({
      title: '提示', //MessageBox 标题
      closeOnClickModal: false,
      message: '是否确定删除当前项?', //MessageBox 消息正文内容
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }).then(async () => {
      // 确定
      const caseTestId = qs.stringify({ caseTestId: row.caseTestId });
      const respone = await CaseTestDeleteCaseTest(caseTestId);
      if (respone.success) onSearch();
      else ElMessage.error(respone.msg)
    }).catch(() => { })


  }

  // 分页
  function handleCurrentChange() {
    onSearch();
  }
  // 监听页面-- 分页显示数量
  function handleSizeChange() {
    onSearch();
  }

  //关闭弹窗
  function ClosePanelVisible(row: any) {
    row.resetFields()
    UpdateVisible.value = false;
  }

  return {
    rules,
    columns,
    loading,
    dataList,
    isdisabled,
    updatefrom,
    pagination,
    QuestionType,
    loadingTitle,
    UpdateVisible,
    handleDelete,
    DetailsUpdate,
    addEvaluation,
    handleSizeChange,
    ClosePanelVisible,
    handleCurrentChange
  }
}
